<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="表名">
              <a-input placeholder="请输入表名" v-model="queryParam.tableName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="检查日期">
              <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.inspectionDate_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.inspectionDate_end"></j-date>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="线别">
                <j-dict-select-tag placeholder="请选择线别" v-model="queryParam.lineType" dictCode="produce_grade"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备名称/检查项目">
                <a-input placeholder="请输入设备名称/检查项目" v-model="queryParam.equipmentName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="表类型编码">
                <j-dict-select-tag placeholder="请选择表类型编码" v-model="queryParam.tableTypeCode" dictCode="facility_temp_type"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="表名称编码">
                <j-dict-select-tag placeholder="请选择表名称编码" v-model="queryParam.tableNameCode" dictCode="facility_name_temp"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('维护保养点检记录主表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
               <a-menu-item>
                <a @click="showModal(record)">记录单</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mesMaintenanceRecordMain-modal ref="modalForm" @ok="modalFormOk"></mesMaintenanceRecordMain-modal>
    <a-modal v-model="visibleDetail" :title="title" @ok="visibleDetail = false" width="90%" :footer="null">
      <ClassBeforEquipmentBehave v-if="['mounter_spot_check','reflow_spot_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <DailySpot v-if="['dvd-rw_check','sub_board_machine_check','automatic_sub_board_machine','wave_soldering_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <DailySpotCheckAndMaintenance v-if="['spi_spot_check','printing_machine_check','aoi_spot_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <IronTemperatureSpotCheck v-if="['soldering_iron_check','tin_furnace_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <ReflowSolderingDailySpotInspection v-if="['reflow_soldering_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <SafetyCheckBeforeWork v-if="['safety_before_work_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <SMTMachineMaintenanceRecord v-if="['rx_7r_spot_check','rs_1_spot_check'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <SMTMachineOperationRecordSheet v-if="['printing_operation_record'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
      <SMTRewindFurnaceOperationRecord v-if="['reflow_operation_record'].indexOf(tableNameCode) > -1" :reportData="reportData"/>
    </a-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MesMaintenanceRecordMainModal from './modules/MesMaintenanceRecordMainModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JDate from '@/components/jeecg/JDate.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'

  import ClassBeforEquipmentBehave from '@/views/reports/ClassBeforEquipmentBehave' // 设备班前点检表（贴片机、回流焊） √
  import DailySpot from '@/views/reports/DailySpot'  // 日常点检记录表(烧录机、半自动分板机、全自动分板机, 波峰焊) √
  import DailySpotCheckAndMaintenance from '@/views/reports/DailySpotCheckAndMaintenance'  // 印刷机日常维护点检表（SPI、印刷机、AOI）√
  import IronTemperatureSpotCheck from '@/views/reports/IronTemperatureSpotCheck'  // 烙铁温度点检表 √
  import ReflowSolderingDailySpotInspection from '@/views/reports/ReflowSolderingDailySpotInspection'  // 回流焊日常点检记录表 √
  import SafetyCheckBeforeWork from '@/views/reports/SafetyCheckBeforeWork'  // 下班前安全检查点检表 √
  import SMTMachineMaintenanceRecord from '@/views/reports/SMTMachineMaintenanceRecord'  // 贴片机保养记录表(RX-7R、RS-1) √
  import SMTMachineOperationRecordSheet from '@/views/reports/SMTMachineOperationRecordSheet'  // SMT印刷机< SPG >作业记录表
  import SMTRewindFurnaceOperationRecord from '@/views/reports/SMTRewindFurnaceOperationRecord'  // SMT回焊炉作业记录表

  export default {
    name: "MesMaintenanceRecordMainList",
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      JDictSelectTag,
      JDate,
      MesMaintenanceRecordMainModal,

      ClassBeforEquipmentBehave,
      DailySpot,
      DailySpotCheckAndMaintenance,
      IronTemperatureSpotCheck,
      ReflowSolderingDailySpotInspection,
      SafetyCheckBeforeWork,
      SMTMachineMaintenanceRecord,
      SMTMachineOperationRecordSheet,
      SMTRewindFurnaceOperationRecord,

    },
    data () {
      return {
        visibleDetail: false,
        reportData: {},
        description: '维护保养点检记录主表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          /*{
            title:'表类型',
            align:"center",
            dataIndex: 'tableType'
          },
          {
            title:'表名',
            align:"center",
            dataIndex: 'tableName'
          },*/
          {
            title:'表类型编码',
            align:"center",
            dataIndex: 'tableTypeCode_dictText'
          },
          {
            title:'表名称编码',
            align:"center",
            dataIndex: 'tableNameCode_dictText'
          },
          {
            title:'检查日期',
            align:"center",
            dataIndex: 'inspectionDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'设备SN',
            align:"center",
            dataIndex: 'equipmentSn'
          },
          {
            title:'设备名称/检查项目',
            align:"center",
            dataIndex: 'equipmentName'
          },
          {
            title:'设备型号/检查位置',
            align:"center",
            dataIndex: 'equipmentModel'
          },
          {
            title:'购置日期/启动检查时间',
            align:"center",
            dataIndex: 'datePurchase',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'线别',
            align:"center",
            dataIndex: 'lineType'
          },
          {
            title:'文件编号/资产编码',
            align:"center",
            dataIndex: 'documentNumber'
          },
          {
            title:'检查方法',
            align:"center",
            dataIndex: 'inspectionMethod'
          },
          {
            title:'检查周期',
            align:"center",
            dataIndex: 'inspectionCycle'
          },
          {
            title:'计入方式',
            align:"center",
            dataIndex: 'accountingMethod'
          },
          {
            title:'注意事项',
            align:"center",
            dataIndex: 'beCareful'
          },
          {
            title:'保养人',
            align:"center",
            dataIndex: 'maintainer'
          },
          {
            title:'稽查人',
            align:"center",
            dataIndex: 'reviewer'
          },
          /*{
            title:'备用1',
            align:"center",
            dataIndex: 'query1'
          },
          {
            title:'备用2',
            align:"center",
            dataIndex: 'query2'
          },
          {
            title:'备用3',
            align:"center",
            dataIndex: 'query3'
          },*/
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/produce/mesMaintenanceRecordMain/list",
          delete: "/produce/mesMaintenanceRecordMain/delete",
          deleteBatch: "/produce/mesMaintenanceRecordMain/deleteBatch",
          exportXlsUrl: "/produce/mesMaintenanceRecordMain/exportXls",
          importExcelUrl: "produce/mesMaintenanceRecordMain/importExcel",
        },
        dictOptions:{},
        titleList: {
          'safety_before_work_check':'下班前安全点检记录表',
          'mounter_spot_check':'贴片机班前点检表',
          'reflow_spot_check':'回流焊班前点检表',
          'printing_machine_check':'印刷机日常维护点检表',
          'spi_spot_check':'SPI日常维护点检表',
          'aoi_spot_check':'AOI日常维护点检表',
          'rx_7r_spot_check':'RX-7R贴片机保养记录表',
          'rs_1_spot_check':'RS-1贴片机保养记录表',
          'reflow_soldering_check':'回流焊日常点检记录表',
          'wave_soldering_check':'波峰焊日常点检记录表',
          'sub_board_machine_check':'半自动分板机日常点检记录表',
          'automatic_sub_board_machine':'全自动分板机日常点检记录表',
          'dvd-rw_check':'烧录机日常点检记录表',
          'soldering_iron_check':'烙铁温度点检表',
          'tin_furnace_check':'锡炉温度点检表',
          'printing_operation_record':'SMT印刷机< SPG >作业记录表',
          'reflow_operation_record':'SMT回焊炉作业记录表',
        },
        title:'',
        tableNameCode:'',
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      showModal(record) {
        let url = ''
        let obj = {}
        if(['printing_operation_record','reflow_operation_record'].indexOf(record.tableNameCode) > -1){
          url = '/produce/mesMaintenanceRecordItem/queryListByMainId'
          obj = {
            mainId : record.id
          }
        }else{
          url = '/produce/mesMaintenanceRecordItem/queryList'
          obj = {
            tableNameCode:record.tableNameCode,
            inspectionDate:record.inspectionDate,
          }
        }
        this.$http.get(url,{
          params:obj
        }).then(res=>{
          if (res.success){
            this.tableNameCode = record.tableNameCode
            let indexs = -1
            if (res.result.dayList){
              res.result.dayCheckName = Object.keys(res.result.dayList)
              res.result.dayCheckList = []
              res.result.dayDealMethords = []
              res.result.ironObj = {
                ironName:[],
                ironCheckLis:[]
              }
              res.result.dayCheckName.forEach(item=>{
                res.result.dayCheckList.push(res.result.dayList[item])
                if(['soldering_iron_check', 'tin_furnace_check'].indexOf(record.tableNameCode) > -1 && ['烙铁编号', '锡炉编号', '点检时间', '设定温度', '实际温度'].indexOf(item) < 0){
                  res.result.ironObj.ironName.push(item)
                  res.result.ironObj.ironCheckLis.push(res.result.dayList[item])
                }
                if (indexs < 0){
                  res.result.dayList[item].forEach((filed,index)=>{
                    if (filed.measureStandard){
                      indexs = index
                    }
                  })
                }
                if (indexs > -1){
                  res.result.dayDealMethords.push(res.result.dayList[item][indexs].measureStandard)
                }
              })
            }
            if (res.result.weekList){
              res.result.weekCheckName = Object.keys(res.result.weekList)
              res.result.weekCheckList = []
              res.result.weekDealMethords = []
              let indexs = -1
              res.result.weekCheckName.forEach(item=>{
                res.result.weekCheckList.push(res.result.weekList[item])
                if (indexs < 0){
                  res.result.weekList[item].forEach((filed,index)=>{
                    if (filed.measureStandard){
                      indexs = index
                    }
                  })
                }
                if (indexs > -1){
                  res.result.weekDealMethords.push(res.result.weekList[item][indexs].measureStandard)
                }
              })
            }
            if (res.result.monthList){
              res.result.monthCheckName = Object.keys(res.result.monthList)
              res.result.monthCheckList = []
              res.result.monthCheckName.forEach(item=>{
                res.result.monthCheckList.push(res.result.monthList[item])
              })
            }
            res.result.title = this.titleList[res.result.tableNameCode]
            this.title = res.result.title
            this.visibleDetail = true;
            this.reportData = res.result
            console.log(222,this.reportData)
          }else {
            this.$message.error(res.message)
          }
        })
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>